<template>
	<view class="chat">
		<view class="report">
			<view class="top">
				<image style="width: 50%;" src="http://applet.atptest.com/2023081701.png" mode="widthFix">
			</view>
			<view class="body">
				<view class="content">
					<view class="left">
						<view class="item">
							<view class="item_title">
								订单编号：
							</view>
							<view class="item_content">
								{{detailInfo.orderNo || ""}}
							</view>
						</view>
						<view class="item">
							<view class="item_title">
								检测型号：
							</view>
							<view class="item_content">
								{{detailInfo.model || ""}}
							</view>
						</view>
						<view class="item">
							<view class="item_title">
								品牌：
							</view>
							<view class="item_content">
								{{detailInfo.brandName || ""}}
							</view>
						</view>
						<view class="item">
							<view class="item_title">
								产地：
							</view>
							<view class="item_content">
								{{detailInfo.production || ""}}
							</view>
						</view>
					</view>
					<view class="right">
						<view class="item">
							<view class="item_title">
								委托方：
							</view>
							<view class="item_content">
								{{detailInfo.client || ""}}
							</view>
						</view>

						<view class="item">
							<view class="item_title">
								封装：
							</view>
							<view class="item_content">
								{{detailInfo.package || ""}}
							</view>
						</view>

						<view class="item">
							<view class="item_title">
								批次：
							</view>
							<view class="item_content">
								{{detailInfo.batch || ""}}
							</view>
						</view>
						<view class="item">
							<view class="item_title">
								等级：
							</view>
							<view class="item_content">
								{{detailInfo.grade || ""}}
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="img_container" v-if="detailInfo.msgList.length > 0">
				<!-- <view class="img_item" v-for="(item,index) in detailInfo.msgList" :key="index">
					<view class="item" v-for="(item1,index1) in item.jsonData" :key="index1">
						<view class="title">
							{{item1.typeName}}
						</view>
						<view class="pic">
							<image style="width:60%;display: inline-block;" v-for="(item2,index2) in item1.imsList"
								:src="item2" :show-menu-by-longpress='true' :key="index2" mode="widthFix"
								@click="prviewImg(item2)">
						</view>
					</view>b 
				</view> -->
				<!-- 轮播图区域 -->
				<swiper class="swiper-container" indicator-color="#fff" :autoplay="false" circular>
					<!-- 轮播项 -->
					<swiper-item v-for="(item,index) in detailInfo.msgList" :key="index">
						<view class="item">
							<image :src="item" :show-menu-by-longpress='true' @click="prviewImg(item)"
								mode="aspectFit"></image>
						</view>
					</swiper-item>
				</swiper>
			</view>

			<!-- <view v-if="currentList.length == count">
				<u-divider text="没有更多了..."></u-divider>
			</view> -->
		</view>
	</view>
</template>

<script>
	import {
		friendlyFormatTime,
		judgeTime
	} from '../../../utils/util.js'
	import {
		mapMutations
	} from 'vuex'
	import {
		GetOpenid,
		InsertMsg_X
	} from '../../../api/user.js'
	import {
		GetPicTure_New,
	} from "../../../api/order.js"
	export default {
		data() {
			return {
				code: '',
				context: '',
				orderNo: "",
				detailInfo: {

				},
				listData: {
					orderNo: '',
					pageIndex: 1,
					size: 10
				},
				messageList: [],
				currentList: [],
				openId: '',
				count: 0,
				imageList: [],
				imageStyles: {
					width: 90,
					height: 90,
				},
				timer: ''
			};
		},
		onLoad: function(option) {
			this.orderNo = option.i
			this.getPicList()
		},
		methods: {
			...mapMutations('user', ['setToken']),

			change(e) {
				// console.log(e);
			},

			btnClick() {
				this.sendMessage({
					orderNo: this.listData.orderNo,
					context: this.context,
				})
				this.context = ''
			},
			//图片预览
			prviewImg(img) {
				console.log("图片",img);
				let urls = []
				let currentImg = 0
				for (let i = 0; i < this.imageList.length; i++) {
					if (img == this.imageList[i]) {
						currentImg = i
					}
					urls.push(this.imageList[i])
				}
				uni.previewImage({
					urls: urls,
					current: currentImg
				});
			},

			//图片列表接口
			async getPicList() {
				const res = await GetPicTure_New({
					orderNo: this.orderNo
				})
				this.detailInfo = res;
				let list = []
				for (let i = 0; i < this.detailInfo.msgList.length; i++) {
					list.push(this.detailInfo.msgList[i])
				}
				this.imageList = list
			},

			// 消列表
			getMessageList(data) {
				uni.showLoading({
					title: '加载中',
				})
				let _that = this
				uni.request({
					url: 'https://wechat.atptest.com:44387/api/PCApplet/ListMsg_Admin',
					method: 'GET',
					data: data,
					success(res) {
						const {
							data
						} = res.data

						if (data) {
							let a = data.items.map((item) => {
								item.typeData.forEach((i) => {
									i.img = i.img.map((j) => {
										return {
											"name": j,
											"extname": j,
											"url": j,
										}
									})
								})
								return item
							})

							_that.count = data.totalCount
							_that.currentList = _that.currentList.concat(a)
							_that.messageList = _that.dataResort(_that.currentList)
							_that.messageList.forEach(el => {
								el.createTime = judgeTime(el.createTimes);
								el.list.forEach(it => {
									it.createTimex = friendlyFormatTime(it.createTime);
								});
							});
						}
						_that.timer = setTimeout(() => {
							uni.hideLoading();
						}, 200)
					}
				})
			},

			getOpenId() {
				GetOpenid().then((res) => {
					this.openId = res
				})
			},

			dataResort(data) {
				// 定义空数组，用于存储新组装的数据
				console.log("data", data);
				let newArr = [];
				// 遍历数组
				data.forEach((item, i) => {
					// 默认当前操作的数据下标 -1
					let index = -1;
					// 判断数组中是否已经存在当前遍历数据的时间
					let isExists = newArr.some((newItem, j) => {
						if (
							item.createTime.substring(0, 10) ==
							newItem.createTime.substring(0, 10)
						) {
							// 存在就保存当前数据下标  用于插入数据
							index = j;
							return true;
						}
					});
					// 如果没有就存储一条新对象数据
					if (!isExists) {
						newArr.push({
							createTime: item.createTime.substring(0, 10),
							createTimes: item.createTime,
							list: [item]
						});
					} else {
						// 如果有就插入到已存在的对象中
						newArr[index].list.push(item);
					}
				});
				console.log("返回新数组", newArr);
				// 返回新数组
				return newArr;
			},

			// 发送消息
			sendMessage(e) {
				InsertMsg_X(e).then((res) => {
					this.listData.pageIndex = 1
					this.currentList = []
					this.messageList = []
					this.getMessageList(this.listData)
				})
			}
		},
		onReachBottom() {
			this.listData.pageIndex++

		}
	}
</script>

<style lang="scss" scoped>
	/**index.wxss**/
	.swiper-container {
		width: 100%;

		.item {
			height: 150px;
		}

		.item image {
			width: 100%;
			height: 100%;
		}
	}

	.chat {
		margin-top: 10rpx;
		padding: 0 50rpx;

		.report {
			padding: 10rpx;
			padding-bottom: 60rpx;
			margin-bottom: 10px;
			border-radius: 8rpx;
			box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.2);

			.top {
				text-align: center;
				margin: 0 20rpx 10rpx;
				border-bottom: 1px solid #e5e5e5;
			}

			.body {
				border-bottom: 1px solid #e5e5e5;

				.content {
					display: flex;
					flex-wrap: wrap;
					padding: 10rpx 10rpx;
					box-sizing: border-box;

					.left {
						width: 60%;
					}

					.right {
						width: 40%;
					}

					.item {
						display: flex;
						width: 100%;
						font-size: 24rpx;
						margin: 20rpx 0;
						padding: 0 10rpx;
						box-sizing: border-box;

						.item_title {
							font-weight: bold;
						}

						.item_content {
							display: flex;
							width: 60%;
							flex: 1;
							word-break: break-all;
							word-wrap: break-word
						}
					}
				}
			}

			.img_container {
				width: 100%;
				display: flex;
				flex-direction: column;
				margin-top: 20rpx;
				padding: 20rpx 0 40rpx 0;
				box-sizing: border-box;

				.img_item {
					margin-bottom: 10rpx;
					border-bottom: 1px solid #eee;

					.item {
						.title {
							font-size: 24rpx;
							text-decoration: underline;
							margin-bottom: 10rpx;
							text-align: center;
						}

						.pic {
							display: flex;
							flex-direction: column;
							align-items: center;
						}
					}
				}
			}
		}

		.input {
			display: flex;
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			padding: 20rpx;
			box-sizing: border-box;
			background-color: #eeeeee;
			border-top: 1px solid #d0d0d0;
		}

		.active {
			color: red;
			font-weight: 500;
			// text-decoration: underline;
		}

		.bule {
			color: green;
			font-weight: 500;
			// text-decoration: underline;
		}

		.imagePopup {
			width: 100%;
			height: 1000rpx;
			margin: 0;
		}

	}

	/deep/ .uni-file-picker__container {
		display: flex;
		flex-wrap: wrap;
		justify-content: left;
	}
</style>